package com.kaizeli.website.controller;

import com.kaizeli.website.mapper.EmployeeMapper;
import com.kaizeli.website.pojo.model.EmployeeDO;
import com.kaizeli.website.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据库测试控制器
 */
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
@Tag(name = "数据库测试", description = "数据库连接和表结构测试接口")
public class DatabaseTestController {
    
    private final EmployeeMapper employeeMapper;
    
    /**
     * 测试数据库连接
     */
    @GetMapping("/database")
    @Operation(summary = "测试数据库连接", description = "测试数据库连接是否正常")
    public Result<String> testDatabase() {
        try {
            List<EmployeeDO> employees = employeeMapper.selectList(null);
            return Result.success("数据库连接正常，员工表记录数: " + employees.size());
        } catch (Exception e) {
            return Result.error(500, "数据库连接失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试员工表结构
     */
    @GetMapping("/employee/structure")
    @Operation(summary = "测试员工表结构", description = "测试员工表字段是否匹配")
    public Result<String> testEmployeeStructure() {
        try {
            // 创建一个测试员工对象
            EmployeeDO testEmployee = new EmployeeDO();
            testEmployee.setEmployeeType("正式");
            testEmployee.setName("测试员工");
            testEmployee.setGender("男");
            testEmployee.setMaritalStatus("未婚");
            testEmployee.setNativePlace("测试市");
            testEmployee.setPoliticalStatus("群众");
            testEmployee.setBirthday(java.time.LocalDate.now());
            testEmployee.setIdCard("123456789012345678");
            testEmployee.setRegisteredAddress("测试地址");
            testEmployee.setPhone("13800138000");
            testEmployee.setWechat("test_wechat");
            testEmployee.setEmail("test@example.com");
            testEmployee.setEducation("本科");
            testEmployee.setMajor("计算机科学");
            testEmployee.setGraduationDate(java.time.LocalDate.now());
            testEmployee.setEmergencyContact("紧急联系人");
            testEmployee.setEmergencyPhone("13900139000");
            
            // 尝试插入
            int result = employeeMapper.insert(testEmployee);
            if (result > 0) {
                // 删除测试数据
                employeeMapper.deleteById(testEmployee.getId());
                return Result.success("员工表结构正常，字段匹配成功");
            } else {
                return Result.error(500, "员工表插入失败");
            }
        } catch (Exception e) {
            return Result.error(500, "员工表结构测试失败: " + e.getMessage());
        }
    }
}